package org.vse.aha.gui.windows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.Lookups;
import org.openide.windows.TopComponent;
import org.vse.aha.common.quotes.IQuotesProvider;
import org.vse.aha.common.quotes.Quote;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.vse.aha.gui.windows//NahodnyVyrok//EN",
autostore = false)
@TopComponent.Description(preferredID = "NahodnyVyrokTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "output", openAtStartup = true)
@ActionID(category = "Window", id = "org.vse.aha.gui.windows.NahodnyVyrokTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_NahodnyVyrokAction",
preferredID = "NahodnyVyrokTopComponent")
@Messages({
  "CTL_NahodnyVyrokAction=NahodnyVyrok",
  "CTL_NahodnyVyrokTopComponent=NahodnyVyrok Window",
  "HINT_NahodnyVyrokTopComponent=This is a NahodnyVyrok window"
})
public final class NahodnyVyrokTopComponent extends TopComponent {

  public NahodnyVyrokTopComponent() {
    initComponents();
    setName(Bundle.CTL_NahodnyVyrokTopComponent());
    setToolTipText(Bundle.HINT_NahodnyVyrokTopComponent());
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelVyrok = new javax.swing.JLabel();
        jLabelAutor = new javax.swing.JLabel();

        setFocusable(true);

        jLabelVyrok.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabelVyrok, org.openide.util.NbBundle.getMessage(NahodnyVyrokTopComponent.class, "NahodnyVyrokTopComponent.jLabelVyrok.text")); // NOI18N

        jLabelAutor.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabelAutor, org.openide.util.NbBundle.getMessage(NahodnyVyrokTopComponent.class, "NahodnyVyrokTopComponent.jLabelAutor.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelVyrok, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelAutor))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabelVyrok)
                .addComponent(jLabelAutor))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelAutor;
    private javax.swing.JLabel jLabelVyrok;
    // End of variables declaration//GEN-END:variables

  @Override
  public void componentOpened() {
    IQuotesProvider pv = Lookup.getDefault().lookup(IQuotesProvider.class);
    Quote výrok = pv.getNáhodnýVýrok();
    jLabelVyrok.setText(výrok.getTextVýroku());
    jLabelAutor.setText(výrok.getAutor());
  }

  @Override
  public void componentClosed() {
    // TODO add custom code on component closing
  }

  @Override
  protected void componentActivated() {
    super.componentActivated();

    // V rámci optimalizace by pochopitelně bylo vhodné následující kód
    // přesunout do soukromé metody okna a volat pouze tuto metodu,
    // aby nedocházelo k duplicitám v kódu.
    IQuotesProvider pv = Lookup.getDefault().lookup(IQuotesProvider.class);
    Quote výrok = pv.getNáhodnýVýrok();
    jLabelVyrok.setText(výrok.getTextVýroku());
    jLabelAutor.setText(výrok.getAutor());
  }

  @Override
  public HelpCtx getHelpCtx() {
    return new HelpCtx(getClass());
  }
  
  
  
  /**
   * Přidá do akcí okna (kontextové menu na záložce okna)
   * akce z layer.xml pro danou cestu, v tomto případě tedy 
   * akci pro vypsání všech výroků do konzole.
   * @return 
   */
  @Override
  public Action[] getActions() {
    List<Action> ca = null;
    ca = new ArrayList<Action>(Arrays.asList(super.getActions()));
    ca.add(null); // Přidání separátoru do menu
    Lookup lkp = Lookups.forPath("Actions/Tools");
    ca.addAll(lkp.lookupAll(Action.class));
    return ca.toArray(new Action[ca.size()]);
  }
  
  void writeProperties(java.util.Properties p) {
    // better to version settings since initial version as advocated at
    // http://wiki.apidesign.org/wiki/PropertyFiles
    p.setProperty("version", "1.0"); //NOI18N
    // TODO store your settings
  }

  void readProperties(java.util.Properties p) {
    String version = p.getProperty("version"); //NOI18N
    // TODO read your settings according to their version
  }
}
